package com.linhui.project.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linhui.project.common.BaseResponse;
import com.linhui.project.common.ErrorCode;
import com.linhui.project.common.ResultUtils;
import com.linhui.project.datasource.*;
import com.linhui.project.exception.BusinessException;
import com.linhui.project.model.dto.post.PostQueryRequest;
import com.linhui.project.model.dto.search.SearchRequest;
import com.linhui.project.model.dto.user.UserQueryRequest;
import com.linhui.project.model.entity.Picture;
import com.linhui.project.model.enums.SearchTypeEnum;
import com.linhui.project.model.vo.PostVO;
import com.linhui.project.model.vo.SearchVO;
import com.linhui.project.model.vo.UserVO;
import com.linhui.project.service.PictureService;
import com.linhui.project.service.PostService;
import com.linhui.project.service.UserService;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * @author: 临晖
 * @since: 2024-05-19
 */

@Component
public class SearchFacade {

    @Resource
    private  PictureDataSource pictureDataSource;

    @Resource
    private  UserDataSource userDataSource;

    @Resource
    private  PostDataSource postDataSource;

    @Resource
    private DataRegister dataRegister;


    public SearchVO searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest httpServletRequest) {
        String searchText = searchRequest.getSearchText();
        String type = searchRequest.getType();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getTypeEnumOnValue(type);


        //如果type为空，则查询所有
        if (searchTypeEnum == null) {
            SearchVO searchVO = new SearchVO();

            //for (SearchTypeEnum typeEnum : SearchTypeEnum.values()) {
            //    DataSource source = DataRegister.getDataSource(typeEnum.getValue());
            //    Page page = source.doSearch(searchText, 1, 10);
            //    searchVO.setData(page.getRecords());
            //}

            CompletableFuture<Page<Picture>> pictureTask = CompletableFuture.supplyAsync(() -> {
                Page<Picture> picturePage = pictureDataSource.doSearch(searchText, 1, 10);
                return picturePage;
            });

            CompletableFuture<Page<UserVO>> userTask = CompletableFuture.supplyAsync(() -> {
                Page<UserVO> userVOPage = userDataSource.doSearch(searchText, 1, 10);
                return userVOPage;
            });

            CompletableFuture<Page<PostVO>> postTask = CompletableFuture.supplyAsync(() -> {

                Page<PostVO> postVOPage = postDataSource.doSearch(searchText, 1, 10);
                return postVOPage;
            });

            CompletableFuture.allOf(pictureTask, userTask, postTask).join();


            try {
                searchVO.setPictureList(pictureTask.get().getRecords());
                searchVO.setUserList(userTask.get().getRecords());
                searchVO.setPostList(postTask.get().getRecords());
                return searchVO;
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询异常");
            }
        } else {
            SearchVO searchVO = new SearchVO();
            DataSource dataSource = dataRegister.getDataSource(searchTypeEnum.getValue());
            Page<Object> page = dataSource.doSearch(searchText, 1, 10);
            searchVO.setDataList(page.getRecords());
            return searchVO;
        }
    }
}
